package top.drinyeit.weilong.model

import android.app.Activity
import android.view.View
import android.widget.Toast
import top.drinyeit.weilong.app.Constant.COMMON_SUCCESS
import top.drinyeit.weilong.presenter.ConfirmOrderPresenter
import top.drinyeit.weilong.retrofit.HttpResponse
import top.drinyeit.weilong.retrofit.RetrofitMethod
import top.drinyeit.weilong.retrofit.subscriber.CommonSubscriber
import top.drinyeit.weilong.retrofit.subscriber.SubscriberListener

/**
 * Created by YJY on 2018/1/13.
 */

data class OrderFatherListBean(var parent_order_info: FatherOrderBean) {

    data class FatherOrderBean(var id: String,
                               var user_id: String,
                               var commodity_total_price: Double,
                               var express_price: Double,
                               var order_price: Double,
                               var consignee_name: String,
                               var consignee_phone: String,
                               var address_detail: String,
                               var order_state: Int,
                               var payment_flag: Int,
                               var payment_type: Int,
                               var payment_time: String,
                               var receipt_time: String,
                               var delete_flag: Int,
                               var create_time: String,
                               var order_title: String,
                               var commodity_total_sum: Int,
                               var reserve_flag: Int,
                               var order_icon: String) {

        fun cancelOrder(view: View) {
            val subscriber = CommonSubscriber<HttpResponse<String>>(object : SubscriberListener<HttpResponse<String>> {
                override fun onError(e: String, code: Int) {
                    Toast.makeText(view.context, e, Toast.LENGTH_SHORT).show()
                }

                override fun onNext(o: HttpResponse<String>) {
                    Toast.makeText(view.context, o.message, Toast.LENGTH_SHORT).show()
                }
            })
            RetrofitMethod.getInstance().cancelOrder(subscriber, id)
        }

        fun goPay(view: View) {
            ConfirmOrderPresenter.showPayDialog(view, order_price, id)
        }

        fun confirmReceive(view: View) {
            val subscriber = CommonSubscriber<HttpResponse<String>>(object : SubscriberListener<HttpResponse<String>> {
                override fun onError(e: String?, code: Int) {
                    Toast.makeText(view.context, "错误，请重新操作！", Toast.LENGTH_SHORT).show()
                }

                override fun onNext(t: HttpResponse<String>?) {
                    if (t?.code == COMMON_SUCCESS) {
                        Toast.makeText(view.context, t?.message, Toast.LENGTH_SHORT).show()
                    } else {
                        Toast.makeText(view.context, t?.message, Toast.LENGTH_SHORT).show()
                    }
                }
            })
            RetrofitMethod.getInstance().confirmReceipt(subscriber, id)

        }

        fun deleteOrder(view: View) {
            val subscriber = CommonSubscriber<HttpResponse<String>>(object : SubscriberListener<HttpResponse<String>> {
                override fun onError(e: String, code: Int) {
                    Toast.makeText(view.context, e, Toast.LENGTH_SHORT).show()
                }

                override fun onNext(o: HttpResponse<String>) {
                    Toast.makeText(view.context, o.message, Toast.LENGTH_SHORT).show()
                }
            })
            RetrofitMethod.getInstance().deleteOrder(subscriber, id)
        }

        fun getOrderState(): String {
            return when (order_state) {
                0 -> "待支付"
                1 -> "待配送"
                2 -> "待签收"
                3 -> "已完成"
                4 -> "关闭"
                else -> ""
            }
        }

        fun getOrderWord(): String {
            return "(含配送费" + express_price + "元)"
        }

        fun getOrderDescribe(): String {
            return order_title + "......等共" + commodity_total_sum + "件商品"
        }
    }
}